//#region 查询部门
let selbm = document.getElementById('selbm');
let btnOk = document.getElementById('btnOk');

function queryBm() {
  ajax.get('/link/departments', {}, (info) => {
    if (!info.success) {
      alert(info.message);
      return;
    }
    for (let i = 0; i < info.data.length; i++) {
      let bm = info.data[i];
      let op = document.createElement('option');
      op.setAttribute('value', bm.deptId);
      op.append(bm.deptName);
      selbm.append(op);
    }
  });
}

//#endregion

//#region 员工联动

let tbWork = document.getElementById('tbWork');

btnOk.addEventListener('click', () => {
  tbWork.innerHTML = '';
  let deptId = selbm.value;
  if (deptId == '') {
    return;
  }
  ajax.get(`/link/employees/${deptId}`, {}, (info) => {
    if (!info.success) {
      alert(info.message);
      return;
    }
    for (let i = 0; i < info.data.length; i++) {
      let worker = info.data[i];
      let tr = document.createElement('tr');
      tbWork.append(tr);
      // 创建员工编号的td
      let td = document.createElement('td');
      td.append(worker.employeeId);
      tr.append(td);
      // 创建员工姓名的td
      td = document.createElement('td');
      td.append(worker.employeeName);
      tr.append(td);
      // 创建员工所属部门的编号的td
      td = document.createElement('td');
      td.append(worker.deptId);
      tr.append(td);
      // 创建员工电话的td
      td = document.createElement('td');
      td.append(worker.phone);
      tr.append(td);
    }
  });
});

queryBm();

//#endregion
